package com.wgo.surveyModel.wiring;

import java.util.Collection;

import com.wgo.bpot.common.transport.exception.db.DatabaseException;
import com.wgo.bpot.server.persist.PersistService;
import com.wgo.bpot.wiring.InitializationService;
import com.wgo.surveyModel.domain.common.SurveyDef;
import com.wgo.surveyModel.domain.server.ejb.SurveyDefEjb;

public class SurveyStartupEjb implements InitializationService {
	private PersistService persistService;

	public SurveyStartupEjb(PersistService persistService) {
		this.persistService = persistService;
	}

	public void initialize() {
        persistService.createSchema();
        persistService.saveOrUpdate(new SurveyDefEjb());
	}

    public boolean isInitializationNeeded() {
      try{
          Collection<SurveyDef> sd = persistService.find(SurveyDef.class, 1);
          return (1 != sd.size());
      } catch (DatabaseException e){
          return true;
      }
  }

}
